<?php
  include_once('./queries/utils.inc.php');

  function filesByJobList($connection, $globalJobId, $username, $pn)
  {
    $fields = array();
    $fields[0] = "src_name";
    $fields[1] = "src_host";
    $fields[2] = "src_path";
    $fields[3] = "src_daemon";
    $fields[4] = "dst_name";
    $fields[5] = "dst_host";
    $fields[6] = "dst_path";
    $fields[7] = "dst_daemon";
    $fields[8] = "last_modified";
    $fields[9] = "size";
    $fields[10] = "checksum";

    $date = TS_FORMAT;

    $sql = "SELECT t.src_name, t.src_host, t.src_path, t.src_daemon, " .
           "t.dst_name, t.dst_host, t.dst_path, t.dst_daemon, " .
           "to_char(t.last_modified, '{$date}') " .
           "AS last_modified, " .   
           "t.transfer_size_bytes, t.checksum " .
           "FROM transfers t, jobs_horizontal_history h " .
           "WHERE t.globaljobid = '{$globalJobId}' " .
           "AND h.owner = '{$username}' " .
           "AND h.globaljobid = t.globaljobid " .
           "ORDER BY last_modified DESC, t.src_name, t.src_host ";

    $rcount = get_recordcount($connection, $sql);
    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function jobsByFileList($connection, $globaljobid, 
                          $username, $pn) {

    $fields = array();
    $fields[0] = "Schedd Name";
    $fields[1] = "Cluster";
    $fields[2] = "Proc";
    $fields[3] = "qdate";
    $fields[4] = "Owner";
    $fields[5] = "Completion Date";
    $fields[6] = "Global Job Id";

    $date = TS_FORMAT;

    $sql = "SELECT DISTINCT h.scheddname, h.cluster_id, h.proc_id, " .
           "to_char(h.qdate, '{$date}'), " .
           "h.owner, " .
           "to_char(h.completiondate, '{$date}'), " .
           "h.globaljobid " .
           "FROM jobs_horizontal_history h " .
           "WHERE h.globaljobid = '{$globaljobid}' " .
           "AND h.owner = '{$username}' " .
           "ORDER BY h.scheddname, h.cluster_id, h.proc_id ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;    
  }

  function machinesByJobList($connection, $globalJobId, $username, $pn)
  {
    // Get the scheddname, cluster and proc for the job
    $jobInfo = getJobInfo($connection, $globalJobId);

    $fields = array();
    $fields[0] = "Machine Id";

    $sql = "SELECT r.machine_id " .
           "FROM runs r, jobs_horizontal_history h " .
           "WHERE r.scheddname = '{$jobInfo[0]}' " .
           "AND r.cluster_id = '{$jobInfo[1]}' " .
           "AND r.proc_id = '{$jobInfo[2]}' " .
           "AND h.owner = '{$username}' " .
           "AND r.scheddname = h.scheddname " .
           "AND r.cluster_id = h.cluster_id " .
           "AND r.proc_id = h.proc_id " .
           "ORDER BY r.machine_id ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function machinesByFileList($connection, $globalJobId, $username, $pn)
  {
    $fields = array();
    $fields[0] = "Machine Id";

    $sql = "SELECT DISTINCT r.machine_id " .
           "FROM runs r " .
           "WHERE r.globaljobid = '{$globalJobId}' ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function jobsByMachineList($connection, $machineId, $username, $pn)
  {
    $fields = array();
    $fields[0] = "Schedd Name";
    $fields[1] = "Cluster";
    $fields[2] = "Proc";
    $fields[3] = "qdate";
    $fields[4] = "Owner";
    $fields[5] = "Completion Date";
    $fields[6] = "Global Job Id";

    $date = TS_FORMAT;

    $sql = "SELECT h.scheddname, h.cluster_id, h.proc_id, " .
           "to_char(h.qdate, '{$date}'), " .
           "h.owner, " .
           "to_char(h.completiondate, '{$date}') " .
           "AS completiondate, h.globaljobid " .
           "FROM jobs_horizontal_history h, matches m " .
           "WHERE m.machine_id = '{$machineId}' " .
           "AND m.globaljobid = h.globaljobid " .
           "AND h.owner = '{$username}' " .
           "ORDER BY completiondate DESC, h.globaljobid ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function filesByMachineList($connection, $machineId, $username, $pn)
  {
    $fields = array();
    $fields[0] = "dst_name";
    $fields[1] = "dst_path";
    $fields[2] = "dst_host";
    $fields[3] = "transfer_time";
    $fields[4] = "last_modified";
    $fields[5] = "size";
    $fields[6] = "checksum";
    $fields[7] = "Global Job Id";

    $date = TS_FORMAT;

    $sql = "SELECT t.dst_name, t.dst_path, t.dst_host, " .
           "to_char(t.transfer_time, '{$date}') " .
           "AS transfer_time, " .
           "to_char(t.last_modified, '{$date}'), " .
           "t.transfer_size_bytes, t.checksum, t.globaljobid " .
           "FROM transfers t, jobs_horizontal_history h, matches m " .
           "WHERE m.machine_id = '{$machineId}' " .
           "AND t.dst_daemon = 'SHADOW' " .
           "AND h.owner = '{$username}' " .
           "AND t.globaljobid = h.globaljobid " .
           "AND t.globaljobid = m.globaljobid " .
           "ORDER BY transfer_time DESC, t.dst_name, t.dst_host ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function jobsInDateRange($connection, $startDate, $endDate, $username, $pn)
  {
    $fields = array();
    $fields[0] = "Schedd Name";
    $fields[1] = "Cluster";
    $fields[2] = "Proc";
    $fields[3] = "qdate";
    $fields[4] = "Completion Date";
    $fields[5] = "Global Job Id";

    $date = TS_FORMAT;

    $sql = "SELECT h.scheddname, h.cluster_id, h.proc_id, " .
           "to_char(h.qdate, '{$date}'), " .
           "to_char(h.completiondate, '{$date}'), " .
           "h.globaljobid " .
           "FROM Jobs_Horizontal_History h " .
           "WHERE to_char(h.qdate, 'yyyy-mm-dd') >= '{$startDate}' " .
           "AND to_char(h.qdate, 'yyyy-mm-dd') <= '{$endDate}' " .
           "AND h.owner = '{$username}' " .
           "ORDER BY h.qdate DESC, h.scheddname, h.cluster_id, h.proc_id ";

    $rcount = get_recordcount($connection, $sql);
    
    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function filesInDateRange($connection, $startDate, $endDate, $username, $pn)
  {
    $fields = array();
    $fields[0] = "Global Job Id";
    $fields[1] = "src_name";
    $fields[2] = "src_path";
    $fields[3] = "src_host";
    $fields[4] = "src_daemon";
    $fields[5] = "dst_name";
    $fields[6] = "dst_path";
    $fields[7] = "dst_host";
    $fields[8] = "dst_daemon";
    $fields[9] = "Last Modified";
    $fields[10] = "Size";
    $fields[11] = "Checksum";
    $fields[12] = "Transfer Time";

    $date = TS_FORMAT;

    $sql = "SELECT DISTINCT h.globaljobid, t.src_name, t.src_path, " .
           "t.src_host, t.src_daemon, t.dst_name, t.dst_path, " .
           "t.dst_host, t.dst_daemon, " .
           "to_char(t.last_modified, '{$date}') " .
           "AS last_modified, " .
           "t.transfer_size_bytes, t.checksum, " .
           "to_char(t.transfer_time, '{$date}') " .
           "AS transfer_time " .
           "FROM transfers t, Jobs_Horizontal_History h " .
           "WHERE to_char(t.last_modified, 'yyyy-mm-dd') >= '{$startDate}'  " .
           "AND to_char(t.last_modified, 'yyyy-mm-dd') <= '{$endDate}' " .
           "AND h.owner = '{$username}' " .
           "AND t.globaljobid = h.globaljobid " .
           "ORDER BY last_modified DESC, t.src_name, t.dst_host ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function filesByNameAndPath($connection, $fileName, $filePath, $username,
                              $pn)
  {
    $fields = array();
    $fields[0] = "Global Job Id";
    $fields[1] = "src_name";
    $fields[2] = "src_path";
    $fields[3] = "src_host";
    $fields[4] = "src_daemon";
    $fields[5] = "dst_name";
    $fields[6] = "dst_path";
    $fields[7] = "dst_host";
    $fields[8] = "dst_daemon";
    $fields[9] = "Last Modified";
    $fields[10] = "Size";
    $fields[11] = "Checksum";
    $fields[12] = "Transfer Time";

    $date = TS_FORMAT;

    $sql = "SELECT DISTINCT h.globaljobid, t.src_name, t.src_path, " .
           "t.src_host, t.src_daemon, t.dst_name, t.dst_path, " .
           "t.dst_host, t.dst_daemon, " .
           "to_char(t.last_modified, '{$date}') " .
           "AS last_modified, " .
           "t.transfer_size_bytes, t. checksum, " .
           "to_char(t.transfer_time, '{$date}') " .
           "AS transfer_time " . 
           "FROM transfers t, Jobs_Horizontal_History h " .
           "WHERE ( (t.src_name LIKE '%{$fileName}%') " .
             "OR (t.dst_name LIKE '%{$fileName}%') ) " .
           "AND ( (t.src_path LIKE '%{$filePath}%') " .
             "OR (t.dst_path LIKE '%{$filePath}%') ) " .
           "AND ( (t.src_path LIKE '%{$filePath}%') " .
             "OR (t.dst_path LIKE '%{$filePath}%') ) " .
           "AND h.owner = '{$username}' " .
           "AND t.globaljobid = h.globaljobid " .
           "ORDER BY last_modified DESC, t.src_name, t.dst_host ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);    

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function jobsByCommandFileNameAndPath($connection, $fileName, $filePath,
                                    $username, $pn)
  {
    $fields = array();
    $fields[0] = "Global Job Id";
    $fields[1] = "File Name";
    $fields[2] = "File Path";
    $fields[3] = "File Host";
    $fields[4] = "File Last Modified";
    $fields[5] = "File Size";
    $fields[6] = "File Checksum";
    $fields[7] = "Job qdate";
    $fields[8] = "Job Completion Date";

    $date = TS_FORMAT;

    // Get the command file info from the submit machine side.
    // Assuming the command file is called condor_exec.exe on the
    //   destination side.
    // The src_daemon field is not being filled in, so just using 
    //   dst_daemon.
    $sql = "SELECT h.globaljobid, t.src_name, t.src_path, t.src_host, " .
           "to_char(t.last_modified, '{$date}') as last_modified, " .
           "t.transfer_size_bytes, t.checksum, to_char(h.qdate, '{$date}'), " .
           "to_char(h.completiondate, '{$date}') " .
           "FROM transfers t, jobs_horizontal_history h " .
           "WHERE t.src_name LIKE '%{$fileName}%' " .
           "AND t.src_path LIKE '%{$filePath}%' " .
           "AND t.dst_daemon = 'STARTER' " .
           "AND t.dst_name = 'condor_exec.exe' " .
           "AND h.owner = '{$username}' " .
           "AND t.globaljobid = h.globaljobid " .
           "ORDER BY last_modified, t.src_name, t.src_host ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function dagJobsList($connection, $dagManJobId, $username, $pn)
  {
    $fields = array();
    $fields[0] = "Schedd Name";
    $fields[1] = "Cluster Id";
    $fields[2] = "Proc Id";
    $fields[3] = "DAG Node Name";
    $fields[4] = "DAG Parent Node Names";

    $recordset = array();

    // Get a set of scheddname, cluster, proc for all nodes that are part
    // of the DAG
    $sql = "SELECT v.scheddname, v.cluster_id, v.proc " .
           "FROM history_vertical v, history_horizontal h " .
           "WHERE v.attr = 'DAGManJobId' " .
           "AND v.val = '{$dagManJobId}' " .
           "AND v.scheddname = h.scheddname " .
           "AND v.cluster_id = h.cluster_id " .
           "AND v.proc = h.proc " .
           "AND h.owner = '{$username}' " .
           "ORDER BY v.scheddname, v.cluster_id, v.proc ";           

    $rcount = get_recordcount($connection, $sql);
    $jobs = get_recordset($connection, $sql);
    
    // For each job, get the DAGNodeName and DAGParentNodeNames
    for($i=0; $i < $rcount; $i++)
    {
      // Put scheddname, cluster, proc into $recordset
      $recordset[$i][0] = $jobs[$i][0];
      $recordset[$i][1] = $jobs[$i][1];
      $recordset[$i][2] = $jobs[$i][2];

      // Get DAGNodeName and put into $recordset
      $sql = "SELECT v.val " .
             "FROM history_vertical v " .
             "WHERE v.scheddname = '{$recordset[$i][0]}' " .
             "AND v.cluster_id = '{$recordset[$i][1]}' " .
             "AND v.proc = '{$recordset[$i][2]}' " . 
             "AND v.attr = 'DAGNodeName' ";
      $x = get_recordcount($connection, $sql);
      if ($x != 1) {
      trigger_error("Error: Not one record for DAGNodeName, scheddname=$recordset[$i][0] cluster=$recordset[$i][1] proc=$recordset[$i][2]", E_USER_ERROR);
      }        
      $DAGNodeName = get_recordset($connection, $sql);
      $recordset[$i][3] = $DAGNodeName[0][0];

      // Get DAGParentNodeNames and put into $recordset
      $sql = "SELECT v.val " .
             "FROM history_vertical v " .
             "WHERE v.scheddname = '{$recordset[$i][0]}' " .
             "AND v.cluster_id = '{$recordset[$i][1]}' " .
             "AND v.proc = '{$recordset[$i][2]}' " . 
             "AND v.attr = 'DAGParentNodeNames' ";
      $x = get_recordcount($connection, $sql);
      if ($x != 1) {
      trigger_error("Error: Not one record for DAGParentNodeNames, scheddname=$recordset[$i][0] cluster=$recordset[$i][1] proc=$recordset[$i][2]", E_USER_ERROR);
      }        
      $DAGParentNodeNames = get_recordset($connection, $sql);
      $recordset[$i][4] = $DAGParentNodeNames[0][0];
    }

    $rs = new recordset();
    $rs->__construct($rcount, $fields, $recordset);
    return $rs;
  }


  function dagBySubmitFileName($connection, $fileName, $username, $pn)
  {
    $fields = array();
    $fields[0] = "DAGMan Id";
    $fields[1] = "Queue Date";
    $fields[2] = "Completion Date";
    $fields[3] = "Global Job Id";

    $sql = "SELECT h.cluster, h.qdate, h.completiondate, h.globaljobid " .
           "FROM history_horizontal h " .
           "WHERE h.args LIKE '%-Dag {$fileName}%' " .
           "AND h.owner = '{$username}' " .
           "AND h.cmd LIKE '%condor_dagman%' " .
           "ORDER BY h.qdate, h.cluster_id ";

    $rcount = get_recordcount($connection, $sql);

    $sql = do_pagination_rewriting($pn, $rcount, $sql);
    //$sql = $sql . get_limitclause($pn, $rcount);

    $rs = new recordset();
    $rs->__construct($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  
  function dagsInDateRange($connection, $startDate, $endDate, $username, $pn)
  {
    $fields = array();
    $fields[0] = "DAGMan Id";
    $fields[1] = "Queue Date";
    $fields[2] = "Completion Date";
    $fields[3] = "Global Job Id";

    $sql = "SELECT h.cluster_id, h.qdate, h.completiondate, h.globaljobid " .
           "FROM history_horizontal h " .
           "WHERE h.qdate >= '{$startDate}' " .
           "AND h.qdate <= '{$endDate}' " .
           "AND h.owner = '{$username}' " .
           "AND h.cmd LIKE '%condor_dagman%' " .
           "ORDER BY h.qdate, h.scheddname, h.cluster_id, h.proc ";

    $rcount = get_recordcount($connection, $sql);
    //echo("Record Count = $rcount ");
    
    $sql = do_pagination_rewriting($pn, $rcount, $sql);
    //$sql = $sql . get_limitclause($pn, $rcount);

    //echo("SQL = $sql");

    $rs = new recordset();
    $rs->__construct($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function dagNodeChildren($connection, $dagManJobId, $dagNodeName, 
                           $username, $pn)
  {
    $fields = array();
    $fields[0] = "Schedd Name";
    $fields[1] = "Cluster Id";
    $fields[2] = "Proc Id";
    $fields[3] = "DAG Node Name";

    $recordset = array();
 
    // Get a set of scheddname, cluster, proc for all nodes that are part
    // of the DAG
    $sql = "SELECT v.scheddname, v.cluster_id, v.proc " .
           "FROM history_vertical v, history_horizontal h " .
           "WHERE v.attr = 'DAGManJobId' " .
           "AND v.val = '{$dagManJobId}' " .
           "AND v.scheddname = h.scheddname " .
           "AND v.cluster_id = h.cluster_id " .
           "AND v.proc = h.proc " .
           "AND h.owner = '{$username}' " .
           "ORDER BY v.scheddname, v.cluster_id, v.proc ";

    $rcount = get_recordcount($connection, $sql);
    $jobs = get_recordset($connection, $sql);

    // For each job, check if it is a child of the given node
    $j = 0;
    for($i=0; $i < $rcount; $i++)
    {
      $sql = "SELECT v.val " .
             "FROM history_vertical v " .
             "WHERE v.scheddname = '{$jobs[$i][0]}' " .
             "AND v.cluster_id =  '{$jobs[$i][1]}' " .
             "AND v.proc =  '{$jobs[$i][2]}' " .
             "AND v.attr = 'DAGParentNodeNames' " .
             "AND v.val LIKE '%{$dagNodeName}%' ";

      $nodeCount = get_recordcount($connection, $sql);
      if ($nodeCount > 1) {
        trigger_error("Error: More than one record for DAGParentNodeNames, scheddname=$jobs[$i][0] cluster=$jobs[$i][1] proc=$jobs[$i][2]", E_USER_ERROR);
      }
      
      // If there is one record then this job is a child of the given node
      if ($nodeCount == 1) {
        // Get the name of this node
        $sql = "SELECT v.val " .
               "FROM history_vertical v " .
               "WHERE v.scheddname = '{$jobs[$i][0]}' " .
               "AND v.cluster_id =  '{$jobs[$i][1]}' " .
               "AND v.proc =  '{$jobs[$i][2]}' " .
               "AND v.attr = 'DAGNodeName' ";
        $countNodeName = get_recordcount($connection, $sql);
        if ($countNodeName != 1) {
          trigger_error("Error: Not one record for DAGNodeName, scheddname=$recordset[$i][0] cluster=$recordset[$i][1] proc=$recordset[$i][2]", E_USER_ERROR);
        }
        $nodeName = get_recordset($connection, $sql);

        // Save the info to the recordset
        $recordset[$j][0] = $jobs[$i][0];
        $recordset[$j][1] = $jobs[$i][1];
        $recordset[$j][2] = $jobs[$i][2];
        $recordset[$j][3] = $nodeName[0][0];
        $j++;
      }
    }
    $rs = new recordset();
    $rs->__construct($j, $fields, $recordset);
    return $rs;
  }

  function dagNodeByName($connection, $nodeName, $username, $pn)
  {
    $fields = array();
    $fields[0] = "DAGMan Id";
    $fields[1] = "DAG Submit File Name";
    $fields[2] = "Node Name";
    $fields[3] = "Node Schedd Name";
    $fields[4] = "Node Cluster Id";
    $fields[5] = "Node Proc Id";
    $fields[6] = "Node Queue Date";
    $fields[7] = "Node Completion Date";
    $fields[8] = "Node GlobalJobId";

    $recordset = array();

    // Get the first chunk of data
    $sql = "SELECT v.val, v.scheddname, v.cluster_id, v.proc, h.qdate, " .
           "h.completiondate, h.globaljobid " .
           "FROM history_vertical v, history_horizontal h " .
           "WHERE v.attr = 'DAGNodeName' " .
           "AND v.val LIKE '%{$nodeName}%' " .
           "AND v.scheddname = h.scheddname " .
           "AND v.cluster_id = h.cluster_id " .
           "AND v.proc = h.proc " .
           "AND h.owner = '{$username}' " .
           "ORDER BY v.scheddname, v.cluster_id, v.proc ";

    $rcount = get_recordcount($connection, $sql);
    $recordset1 = get_recordset($connection, $sql);

    // For each node in $recordset1 get the DAGMan Id and 
    // DAG submit file name
    for ($i=0; $i < $rcount; $i++) {
      $sql = "SELECT v.val " .
             "FROM history_vertical v " . 
             "WHERE v.scheddname = '{$recordset1[$i][1]}' " .
             "AND v.cluster_id =  '{$recordset1[$i][2]}' " .
             "AND v.proc =  '{$recordset1[$i][3]}' " .
             "AND v.attr = 'DAGManJobId' ";

      $idCount = get_recordcount($connection, $sql);
      if ($idCount != 1) {
        trigger_error("Error: Not one record for DAGManJobId, scheddname=$recordset1[$i][1] cluster=$recordset1[$i][2] proc=$recordset1[$i][3]", E_USER_ERROR);
      }
      $dagManJobId = get_recordset($connection, $sql);

      $sql = "SELECT h.args " .
             "FROM history_horizontal h " .
             "WHERE h.scheddname = '{$recordset1[$i][1]}' " .
             "AND h.cluster_id = '{$dagManJobId[0][0]}' ";
      $argsCount = get_recordcount($connection, $sql);
      if ($argsCount != 1) {
        trigger_error("Error: Not one record for DAGMan args, scheddname=$recordset1[$i][1] cluster=$dagManJobId[0][0]", E_USER_ERROR);
      }
      $args = get_recordset($connection, $sql);
      $submitFileName = parseArgs($args[0][0]);

      // Save fields to the recordset
      $recordset[$i][0] = $dagManJobId[0][0];
      $recordset[$i][1] = $submitFileName;
      $recordset[$i][2] = $recordset1[$i][0];
      $recordset[$i][3] = $recordset1[$i][1];
      $recordset[$i][4] = $recordset1[$i][2];
      $recordset[$i][5] = $recordset1[$i][3];
      $recordset[$i][6] = $recordset1[$i][4];
      $recordset[$i][7] = $recordset1[$i][5];
      $recordset[$i][8] = $recordset1[$i][6];
    }

    $rs = new recordset();
    $rs->__construct($rcount, $fields, $recordset);
    return $rs;

  }

  function dagNodeByDate($connection, $startDate, $endDate, $username,
                         $pn)
  {
    $fields = array();
    $fields[0] = "DAGMan Id";
    $fields[1] = "DAG Submit File Name";
    $fields[2] = "Node Name";
    $fields[3] = "Node Schedd Name";
    $fields[4] = "Node Cluster Id";
    $fields[5] = "Node Proc Id";
    $fields[6] = "Node Queue Date";
    $fields[7] = "Node Completion Date";
    $fields[8] = "Node GlobalJobId";

    $recordset = array();
 
    // Get the first chunk of data
    $sql = "SELECT v.val, v.scheddname, v.cluster_id, v.proc, h.qdate, " .
           "h.completiondate, h.globaljobid " .
           "FROM history_vertical v, history_horizontal h " .
           "WHERE v.attr = 'DAGNodeName' " .
           "AND v.scheddname = h.scheddname " .
           "AND v.cluster_id = h.cluster_id " .
           "AND v.proc = h.proc " .
           "AND h.qdate >= '{$startDate}' ".
           "AND h.qdate <= '{$endDate}' ".
           "AND h.owner = '{$username}' " .
           "ORDER BY v.scheddname, v.cluster_id, v.proc ";

    $rcount = get_recordcount($connection, $sql);
    $recordset1 = get_recordset($connection, $sql);

    // For each node in $recordset1 get the DAGMan Id and 
    // DAG submit file name
    for ($i=0; $i < $rcount; $i++) {
      $sql = "SELECT v.val " .
             "FROM history_vertical v " . 
             "WHERE v.scheddname = '{$recordset1[$i][1]}' " .
             "AND v.cluster_id =  '{$recordset1[$i][2]}' " .
             "AND v.proc =  '{$recordset1[$i][3]}' " .
             "AND v.attr = 'DAGManJobId' ";

      $idCount = get_recordcount($connection, $sql);
      if ($idCount != 1) {
        trigger_error("Error: Not one record for DAGManJobId, scheddname=$recordset1[$i][1] cluster=$recordset1[$i][2] proc=$recordset1[$i][3]", E_USER_ERROR);
      }
      $dagManJobId = get_recordset($connection, $sql);

      $sql = "SELECT h.args " .
             "FROM history_horizontal h " .
             "WHERE h.scheddname = '{$recordset1[$i][1]}' " .
             "AND h.cluster_id = '{$dagManJobId[0][0]}' ";
      $argsCount = get_recordcount($connection, $sql);
      if ($argsCount != 1) {
        trigger_error("Error: Not one record for DAGMan args, scheddname=$recordset1[$i][1] cluster=$dagManJobId[0][0]", E_USER_ERROR);
      }
      $args = get_recordset($connection, $sql);
      $submitFileName = parseArgs($args[0][0]);

      // Save fields to the recordset
      $recordset[$i][0] = $dagManJobId[0][0];
      $recordset[$i][1] = $submitFileName;
      $recordset[$i][2] = $recordset1[$i][0];
      $recordset[$i][3] = $recordset1[$i][1];
      $recordset[$i][4] = $recordset1[$i][2];
      $recordset[$i][5] = $recordset1[$i][3];
      $recordset[$i][6] = $recordset1[$i][4];
      $recordset[$i][7] = $recordset1[$i][5];
      $recordset[$i][8] = $recordset1[$i][6];
    }

    $rs = new recordset();
    $rs->__construct($rcount, $fields, $recordset);
    return $rs;
  }

  function dagNodeParent($connection, $dagManJobId, $dagNodeName,
                         $username, $pn)
  {
    $fields = array();
    $fields[0] = "Schedd Name";
    $fields[1] = "Cluster Id";
    $fields[2] = "Proc Id";
    $fields[3] = "DAG Node Name";

    $recordset = array();
    $parentCount = 0;

    // Get schedd, cluster, proc, parent node names for this node
    $sql = "SELECT v1.scheddname, v1.cluster_id, v1.proc, v3.val " .
           "FROM history_vertical v1, history_vertical v2, " .
           "history_vertical v3 " .
           "WHERE v1.attr = 'DAGNodeName' " .
           "AND v1.val = '{$dagNodeName}' " .
           "AND v2.attr = 'DAGManJobId' " .
           "AND v2.val = '{$dagManJobId}' " .
           "AND v1.scheddname = v2.scheddname " .
           "AND v1.cluster_id = v2.cluster_id " .
           "AND v1.proc = v2.proc " .
           "AND v3.attr = 'DAGParentNodeNames' " .
           "AND v2.scheddname = v3.scheddname " .
           "AND v2.cluster_id = v3.cluster_id " .
           "AND v2.proc = v3.proc ";

    $nodeCount = get_recordcount($connection, $sql);
    if ($nodeCount != 1) {
      trigger_error("Error: Not one record for DAGParentNodeNames, scheddname=$jobs[$i][0] cluster=$jobs[$i][1] proc=$jobs[$i][2]", E_USER_ERROR);
    }
    $nodeInfo = get_recordset($connection, $sql);

    // Parse the parent node names
    // If there are multiple parent nodes they are seperated with a comma
    // For each parent node, get its info and put the info into $recordset
    $parent = strtok($nodeInfo[0][3], ",");

    // If parent is FALSE then there is none or one parent
    if ($parent === FALSE) {
      $parentCount = 0;

      // If the parent nodes field is empty then there are no parents
      // for the given node. Do nothing and let no records display.
      // Otherwise, get the info for the single parent node.
      if ($nodeInfo[0][3] != "") {
        $parentCount = 1;
        $sql = "SELECT v2.scheddname, v2.cluster_id, v2.proc, v2.val " .
               "FROM history_vertical v1, history_vertical v2 " .
               "WHERE v1.scheddname = '{$nodeInfo[0][0]}' " .
               "AND v1.attr = 'DAGManJobId' " .
               "AND v1.val = '{$dagManJobId}' " .
               "AND v2.attr = 'DAGNodeName' " .
               "AND v2.val = '{$nodeInfo[0][3]}' " .
               "AND v1.scheddname = v2.scheddname " .
               "AND v1.cluster_id = v2.cluster_id " .
               "AND v1.proc = v2.proc ";
        $count = get_recordcount($connection, $sql);
        if ($count != 1) {
        trigger_error("Error: Not one record for DAG Parent Info, scheddname=$jobs[$i][0] cluster=$jobs[$i][1] proc=$jobs[$i][2]", E_USER_ERROR);
        }
        $record = get_recordset($connection, $sql);
        $recordset[0][0] = $record[0][0];
        $recordset[0][1] = $record[0][1];
        $recordset[0][2] = $record[0][2];
        $recordset[0][3] = $record[0][3];
      }
    }
    else { // there is more than one parent node, get the info for each.
      $parentCount = 0;
      while($parent !== FALSE) {
        $sql = "SELECT v2.scheddname, v2.cluster_id, v2.proc, v2.val " .
               "FROM history_vertical v1, history_vertical v2 " .
               "WHERE v1.scheddname = '{$nodeInfo[0][0]}' " .
               "AND v1.attr = 'DAGManJobId' " .
               "AND v1.val = '{$dagManJobId}' " .
               "AND v2.attr = 'DAGNodeName' " .
               "AND v2.val = '{$parent}' " .
               "AND v1.scheddname = v2.scheddname " .
               "AND v1.cluster_id = v2.cluster_id " .
               "AND v1.proc = v2.proc ";
        $count = get_recordcount($connection, $sql);
        if ($count != 1) {
        trigger_error("Error: Not one record for DAG Parent Info, scheddname=$jobs[$i][0] cluster=$jobs[$i][1] proc=$jobs[$i][2]", E_USER_ERROR);
        }
        $record = get_recordset($connection, $sql);
        $recordset[$parentCount][0] = $record[0][0];
        $recordset[$parentCount][1] = $record[0][1];
        $recordset[$parentCount][2] = $record[0][2];
        $recordset[$parentCount][3] = $record[0][3];

        $parent = strtok(",");
        $parentCount++;
      }
    }
    $rs = new recordset();
    $rs->__construct($parentCount, $fields, $recordset);
    return $rs;
  }

  // Returns an array with the schedd name, cluster, and proc for the job
  function getJobInfo($connection, $globaljobid)
  {
    $jobInfo = array();
    
    $sql = "SELECT h.scheddname, h.cluster_id, h.proc_id " .
           "FROM jobs_horizontal_history h " .
           "WHERE h.globaljobid = '{$globaljobid}'";

    $rcount = get_recordcount($connection, $sql);
    if ($rcount > 1) {
      trigger_error("Error: More than one record with Global Job Id of $globaljobid", E_USER_ERROR);
    }
    
    $data = get_recordset($connection, $sql);
    $jobInfo[0] = $data[0][0];
    $jobInfo[1] = $data[0][1];
    $jobInfo[2] = $data[0][2];

    return $jobInfo;
  }

  // Parse the args field to extract the DAG submit file name.
  // The submit file name is found as: "-Dag submitFileName"
  function parseArgs($args)
  {
    preg_match("/-Dag\s(\S+)/", $args, $match);
    $submitFileName = $match[1];
    return $submitFileName;
  }

?>
